package daos;

import java.util.List;

import javax.management.Query;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import entidades.Professor;

public class DAOprofessor {

	public void salvar (Professor a){
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("escola_2");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		em.persist(a);
		em.getTransaction().commit();
	em.close();
	emf.close();
		
	}
	public void alterar(Professor a){
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("escola_2");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		em.merge(a) ;
		em.getTransaction().commit();
	em.close();
	emf.close();
	}
	public List<Professor> listar(){
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("escola_2");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		Query b = (Query) em.createQuery("SELECT c FROM Professor c");
		@SuppressWarnings("unchecked")
		List<Professor> lista = (List<Professor>)((javax.persistence.Query) b).getResultList();
		em.getTransaction().commit();
		em.close();
		emf.close();
		
		return lista;
	}
	
}
